package com.ext.aic.apis.attachment;

import com.ext.aic.AICApplication;
import com.ext.aic.apis.ApiPaths;
import com.ext.aic.beans.response.PreUploadResponse;
import com.ext.aic.constants.ApiConstants;
import com.ext.aic.utils.UserUtils;
import com.ext.aic.utils.http.HttpUtils;
import com.eyinfo.android_pure_utils.utils.JsonUtils;
import com.eyinfo.android_pure_utils.utils.PathsUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class PreUploadRequest {

    protected void onPreUploadSuccess(PreUploadResponse response) {

    }

    protected void onPreUploadFailed(String resourceName) {

    }

    public void request(String resourceName) {

//        PreUpdateService preUpdateService = new PreUpdateService() {
//            @Override
//            protected void onPreUpdateSuccess(PreUploadResponse response) {
//                PreUploadRequest.this.onPreUploadSuccess(response);
//            }
//
//            @Override
//            protected void onPreUpdateFail() {
//                PreUploadRequest.this.onPreUploadFailed(resourceName);
//            }
//        };
//        preUpdateService.preUpload(resourceName);

//        ParamsBuilder builder = new ParamsBuilder();
//        builder.add("resourceName", resourceName);
//        ApiDefine apiDefine = new ApiDefine(ApiPaths.preUpload, builder);

        String url = PathsUtils.combine(ApiConstants.baseUrlForBusiness, ApiPaths.preUpload);
        url += "?resourceName=" + resourceName;
        Request.Builder builder = new Request.Builder().url(url).get();
        builder.addHeader("Authorization", "Bearer " + UserUtils.getInstance().getAccessToken());
        Request request = builder.build();
        OkHttpClient client = HttpUtils.getInstance().getClient(AICApplication.getInstance().getApplicationContext());
        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseBody = response.body().string();
                    PreUploadResponse uploadResponse = JsonUtils.parseT(responseBody, PreUploadResponse.class);
                    onPreUploadSuccess(uploadResponse);
                } else {
                    onPreUploadFailed(resourceName);
                }
            }

            @Override
            public void onFailure(Call call, IOException e) {
                onPreUploadFailed(resourceName);
            }
        });
    }
}
